<?php
/**
 * @ClassName: Equipmentcostrecords
 * @Description: TODO
 * @author: gydtrade
 * @Date: 2025-11-20 16:27
 * @Version: V1.0.0
 */

namespace app\admin\controller;

use app\common\enum\CommonEnum;
use app\common\model;
use think\Controller;
use tpext\builder\traits\HasBuilder;

class Equipmentcostrecords extends Controller
{
    use HasBuilder;

    protected $dataModel;
    protected $device_id;

    protected function initialize()
    {
        parent::initialize();

        $this->dataModel = new model\EquipmentCostRecords();
        $this->pageTitle = '设备费用记录';
        $this->pagesize = 10;

        $this->indexWith = ['quipment'];

        $this->device_id = input('device_id', 0);
    }

    /**
     * 构建搜索
     *
     * @return void
     */
    protected function buildSearch()
    {
        $search = $this->search;

        if ($this->device_id) {
            $search->select('device_id', '设备')->dataUrl(url('/admin/equipment/selectPage'))->default($this->device_id)->readonly();
        } else {
            $search->select('device_id', '设备')->dataUrl(url('/admin/equipment/selectPage'))->default($this->device_id);
        }
        $search->select('holder_type', '设备持有人')->options(model\Equipment::$holder_types)->when(model\Equipment::holder_type_center)->with(
            $search->select('related_id', '运营中心', 12)->dataUrl(url('/admin/agent.operation/selectPage')),
        )->when(model\Equipment::holder_type_agent)->with(
            $search->select('related_id', '代理', 12)->dataUrl(url('/admin/agent.agent/selectPage')),
        )->when(model\Equipment::holder_type_store)->with(
            $search->select('related_id', '门店', 12)->dataUrl(url('/admin/store.store/selectPage')),
        );
        $search->select('type', '记录类型')->options(model\EquipmentCostRecords::$types);
        $search->select('trade_type', '交易类型')->options(CommonEnum::tradeTypes(true));

        $search->datetime('start', '支付时间')->placeholder('起始');
        $search->datetime('end', '~')->placeholder('截止');
    }

    /**
     * 构建搜索条件
     * @return array
     */
    protected function filterWhere()
    {
        $searchData = request()->get();

        $where = [];
        if (isset($searchData['device_id']) && $searchData['device_id'] != '') {
            $where[] = ['device_id', '=', $searchData['device_id']];
        }
        if (isset($searchData['holder_type']) && $searchData['holder_type'] != '') {
            $where[] = ['holder_type', '=', $searchData['holder_type']];
        }
        if (isset($searchData['related_id']) && $searchData['related_id'] != '') {
            $where[] = ['related_id', '=', $searchData['related_id']];
        }
        if (isset($searchData['type']) && $searchData['type'] != '') {
            $where[] = ['type', '=', $searchData['type']];
        }
        if (isset($searchData['trade_type']) && $searchData['trade_type'] != '') {
            $where[] = ['trade_type', '=', $searchData['trade_type']];
        }
        if (!empty($searchData['start'])) {
            $where[] = ['create_time', 'egt', $searchData['start']];
        }
        if (!empty($searchData['end'])) {
            $where[] = ['create_time', 'elt', $searchData['end']];
        }

        return $where;
    }

    /**
     * 构建表格
     *
     * @return void
     */
    protected function buildTable(&$data = [])
    {
        $table = $this->table;

        $table->show('id', 'ID');
        $table->match('device_id', '设备')->to('{device_id}#{quipment.name}');
        $table->show('holder_info', '交易人');
        $table->match('type', '记录类型')->options(model\EquipmentCostRecords::$types);
        $table->show('amount', '交易金额');
        $table->match('trade_type', '交易类型')->options(CommonEnum::tradeTypes(true));
        $table->match('status', '交易状态')->options([0 => '失败', 1 => '成功'])->mapClassGroup([[0, 'danger'], [1, 'success']]);
        $table->show('remark', '备注');
        $table->show('create_time', '交易时间');

        $table->sortable('id, create_time');

        $table->getToolbar()
            ->btnRefresh();

        $table->getActionbar()
            ->btnView();
    }

    /**
     * 构建表单
     *
     * @param boolean $isEdit
     * @param array $data
     */
    protected function buildForm($isEdit, &$data = [])
    {
        $form = $this->form;

        $form->hidden('id');
        $form->match('device_id', '设备')->to('{device_id}#{quipment.name}');
        $form->show('holder_info', '交易人');
        $form->match('type', '记录类型')->options(model\EquipmentCostRecords::$types);
        $form->show('amount', '交易金额');
        $form->match('trade_type', '交易类型')->options(CommonEnum::tradeTypes(true));
        $form->match('status', '交易状态')->options([0 => '失败', 1 => '成功'])->mapClassGroup([[0, 'danger'], [1, 'success']]);
        $form->show('remark', '备注');
        $form->show('create_time', '交易时间');
    }

    /**
     * 保存数据
     *
     * @param integer $id
     * @return void
     */
    private function save($id = 0)
    {
        $data = request()->post();

        $result = $this->validate($data, []);

        if (true != $result) {
            $this->error($result);
        }

        if ($id) {
            // 编辑
            $data['update_time'] = date_now();
        } else {
            // 新增
            $data['create_time'] = date_now();
            $data['update_time'] = date_now();
        }

        return $this->doSave($data, $id);
    }
}